﻿Imports System.Data
' BS.Utility.RunClientScriptForAjax(Me.Page, "setTimeout(""$get('" & gvMain.Rows(CInt(ht("SelectIndex"))).FindControl("rdo_gvMain").ClientID & "').focus(); "", 100);")
Partial Class WebForm_AssetMaintain_Component_AssetTransfer
    Inherits BaseUserControl
    Public Event onShowNext(ByVal source As Object, ByVal e As EventArgs)
    Public Event onShow(ByVal source As Object, ByVal e As EventArgs)
    Public Event onShowPreviouse(ByVal source As Object, ByVal e As EventArgs)
    Public Sub IniData(ByVal assetdef_id As String, ByVal asset_serialNo As String, ByVal dbName As String, ByVal UID As String)
        ViewState("assetdef_id") = assetdef_id

        ViewState("asset_serialNo") = asset_serialNo
        hiDBName.Value = dbName
        ViewState("UID") = UID
        ddlassetdef_id.DataBind()
        ddlassetdef_id.SelectedValue = assetdef_id

        txtasset_owner.Text = UID
        txtasset_serialNo.Text = asset_serialNo
    End Sub
    Public Property myDT() As Datatable
        Get
            If ViewState("myDT") Is Nothing Then
                Dim dt As DataTable
                dt = New DataTable()
                dt.Columns.Add("asset_serialNo", System.Type.GetType("System.String"))
                dt.Columns.Add("Def_Name", System.Type.GetType("System.String"))
                dt.Columns.Add("ModiType", System.Type.GetType("System.String"))
                dt.Columns.Add("asset_owner", System.Type.GetType("System.String"))
                dt.Columns.Add("asset_ownerID", System.Type.GetType("System.String"))
                dt.Columns.Add("User2", System.Type.GetType("System.String"))
                dt.Columns.Add("NewOwner", System.Type.GetType("System.String"))
                dt.Columns.Add("NewUser2", System.Type.GetType("System.String"))
                dt.Columns.Add("OtherUser", System.Type.GetType("System.String"))

                ViewState("myDT") = dt
                Return ViewState("myDT")
            Else
                Return ViewState("myDT")
            End If
        End Get
        Set(ByVal value As DataTable)
            ViewState("myDT") = value

        End Set
    End Property

    Public Property myOtherDT() As DataTable
        Get
            If ViewState("myOtherDT") Is Nothing Then
                Dim dt As DataTable
                dt = New DataTable()
                dt.Columns.Add("asset_serialNo", System.Type.GetType("System.String"))
                dt.Columns.Add("Def_Name", System.Type.GetType("System.String"))
                dt.Columns.Add("ModiType", System.Type.GetType("System.String"))
                dt.Columns.Add("asset_owner", System.Type.GetType("System.String"))
                dt.Columns.Add("asset_ownerID", System.Type.GetType("System.String"))
                dt.Columns.Add("User2", System.Type.GetType("System.String"))
                dt.Columns.Add("NewOwner", System.Type.GetType("System.String"))
                dt.Columns.Add("NewUser2", System.Type.GetType("System.String"))
                dt.Columns.Add("OtherUser", System.Type.GetType("System.String"))

                ViewState("myOtherDT") = dt
                Return ViewState("myOtherDT")
            Else
                Return ViewState("myOtherDT")
            End If
        End Get
        Set(ByVal value As DataTable)
            ViewState("myOtherDT") = value
        End Set
    End Property

    Public Sub RemoveData()
        ViewState.Remove("myOtherDT")
        ViewState.Remove("myDT")
        gvMain.DataBind()
    End Sub

    Public Sub FreshData()
        gvMain.DataBind()
    End Sub
    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        'InsertData()

        gvMain.DataBind()

        RaiseEvent onShow(sender, New EventArgs())
    End Sub

    'Protected Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConfirm.Click
    '    Dim dr As DataRow
    '    Dim drs() As DataRow
    '    Dim drs1() As DataRow
    '    For Each gr As GridViewRow In gvMain.Rows
    '        If CType(gr.FindControl("chkTransfer"), CheckBox).Checked Then
    '            '檢杳是否已在datable 中
    '            drs = myDT.Select("asset_serialNo='" + gr.Cells(2).Text + "'")
    '            If drs.Length = 0 Then
    '                drs1 = myOtherDT.Select("asset_serialNo='" + gr.Cells(2).Text + "'") ''檢查是否在移轉資料中
    '                If drs1.Length = 0 Then
    '                    If CType(gr.FindControl("txtNewOwner"), TextBox).Text.Trim = "" Then

    '                        ShowMessageForAjax(btnConfirm, GetLocalResourceObject("NewOwnerEmpty"))
    '                        RaiseEvent onShow(sender, New EventArgs())
    '                        Exit Sub
    '                    End If

    '                    dr = myDT.NewRow()
    '                    dr("asset_serialNo") = HttpUtility.HtmlDecode(gr.Cells(2).Text).Trim ''資產編號
    '                    dr("Def_Name") = HttpUtility.HtmlDecode(gr.Cells(1).Text).Trim '資產類別
    '                    dr("ModiType") = "1"  '移轉他人
    '                    dr("asset_owner") = HttpUtility.HtmlDecode(gr.Cells(3).Text).Trim
    '                    dr("User2") = HttpUtility.HtmlDecode(gr.Cells(4).Text).Trim
    '                    dr("NewOwner") = CType(gr.FindControl("txtNewOwner"), TextBox).Text
    '                    dr("NewUser2") = CType(gr.FindControl("txtNewUser2"), TextBox).Text
    '                    dr("OtherUser") = CType(gr.FindControl("txtNewOtherUser"), TextBox).Text
    '                    myDT.Rows.Add(dr)
    '                End If

    '            End If


    '        End If
    '    Next

    '    For Each gr As GridViewRow In gvMain.Rows
    '        If CType(gr.FindControl("chkReturn"), CheckBox).Checked Then
    '            '檢杳是否已在datable 中
    '            drs = myDT.Select("asset_serialNo='" + gr.Cells(2).Text + "'")
    '            If drs.Length = 0 Then
    '                drs1 = myOtherDT.Select("asset_serialNo='" + gr.Cells(2).Text + "'") ''檢查是否在移轉資料中
    '                If drs1.Length = 0 Then
    '                    If CType(gr.FindControl("txtNewOwner"), TextBox).Text.Trim = "" Then
    '                        ShowMessageForAjax(btnConfirm, GetLocalResourceObject("NewOwnerEmpty"))
    '                        RaiseEvent onShow(sender, New EventArgs())
    '                        Exit Sub
    '                    End If
    '                    dr = myDT.NewRow()
    '                    dr("asset_serialNo") = HttpUtility.HtmlDecode(gr.Cells(2).Text).Trim ''資產編號
    '                    dr("Def_Name") = HttpUtility.HtmlDecode(gr.Cells(1).Text).Trim '資產類別
    '                    dr("ModiType") = "2"  '繳回
    '                    dr("asset_owner") = HttpUtility.HtmlDecode(gr.Cells(3).Text).Trim
    '                    dr("User2") = HttpUtility.HtmlDecode(gr.Cells(4).Text).Trim
    '                    dr("NewOwner") = CType(gr.FindControl("txtNewOwner"), TextBox).Text
    '                    dr("NewUser2") = CType(gr.FindControl("txtNewUser2"), TextBox).Text
    '                    dr("OtherUser") = CType(gr.FindControl("txtNewOtherUser"), TextBox).Text
    '                    myDT.Rows.Add(dr)
    '                End If

    '            End If


    '        End If
    '    Next


    '    'ShowMessageForAjax(btnClose, GetLocalResourceObject("Finish"))

    'End Sub
    Private Function InsertData() As Boolean
        Dim dr As DataRow
        Dim drs() As DataRow
        Dim drs1() As DataRow
        Dim uDAO As OAMS.DAO.UsersDAO = New OAMS.DAO.UsersDAO(hiDBName.Value)
        Dim empNo As String

        For Each gr As GridViewRow In gvMain.Rows
            If CType(gr.FindControl("chkTransfer"), CheckBox).Checked Then
                empNo = CType(gr.FindControl("txtNewOwner"), TextBox).Text.Trim
                If empNo <> "IT" Then
                    If Not uDAO.UserIsExist(empNo) Then
                        ShowMessageForAjax(btnConfirm, GetLocalResourceObject("NewOwnerNotExist"))
                        RaiseEvent onShow(btnConfirm, New EventArgs())
                        Return False
                    End If
                End If
            End If
        Next
        For Each gr As GridViewRow In gvMain.Rows
            If CType(gr.FindControl("chkTransfer"), CheckBox).Checked Then
                empNo = CType(gr.FindControl("txtNewOwner"), TextBox).Text.Trim
                If empNo <> "IT" Then
                    If uDAO.UserIsQuit(empNo) Then
                        ShowMessageForAjax(btnConfirm, GetLocalResourceObject("NewOwnerQuit"))
                        RaiseEvent onShow(btnConfirm, New EventArgs())
                        Return False
                    End If
                End If
            End If
        Next
        For Each gr As GridViewRow In gvMain.Rows
            If CType(gr.FindControl("chkTransfer"), CheckBox).Checked Then
                '檢杳是否已在datable 中
                drs = myDT.Select("asset_serialNo='" + gr.Cells(2).Text + "'")
                If drs.Length = 0 Then
                    drs1 = myOtherDT.Select("asset_serialNo='" + gr.Cells(2).Text + "'") ''檢查是否在移轉資料中
                    If drs1.Length = 0 Then
                        If CType(gr.FindControl("txtNewOwner"), TextBox).Text.Trim = "" Then

                            ShowMessageForAjax(btnConfirm, GetLocalResourceObject("NewOwnerEmpty"))
                            RaiseEvent onShow(btnConfirm, New EventArgs())
                            Return False
                        End If

                        dr = myDT.NewRow()
                        dr("asset_serialNo") = HttpUtility.HtmlDecode(gr.Cells(2).Text).Trim ''資產編號
                        dr("Def_Name") = HttpUtility.HtmlDecode(gr.Cells(1).Text).Trim '資產類別
                        dr("ModiType") = "1"  '移轉他人
                        dr("asset_owner") = HttpUtility.HtmlDecode(gr.Cells(3).Text).Trim
                        dr("User2") = HttpUtility.HtmlDecode(gr.Cells(4).Text).Trim
                        dr("NewOwner") = CType(gr.FindControl("txtNewOwner"), TextBox).Text
                        dr("NewUser2") = CType(gr.FindControl("txtNewUser2"), TextBox).Text
                        dr("OtherUser") = CType(gr.FindControl("txtNewOtherUser"), TextBox).Text
                        dr("asset_ownerID") = HttpUtility.HtmlDecode(gr.Cells(8).Text).Trim
                        myDT.Rows.Add(dr)
                    End If

                Else
                    drs(0)("asset_serialNo") = HttpUtility.HtmlDecode(gr.Cells(2).Text).Trim ''資產編號
                    drs(0)("Def_Name") = HttpUtility.HtmlDecode(gr.Cells(1).Text).Trim '資產類別
                    drs(0)("ModiType") = "1"  '移轉他人
                    drs(0)("asset_owner") = HttpUtility.HtmlDecode(gr.Cells(3).Text).Trim
                    drs(0)("User2") = HttpUtility.HtmlDecode(gr.Cells(4).Text).Trim
                    drs(0)("NewOwner") = CType(gr.FindControl("txtNewOwner"), TextBox).Text
                    drs(0)("NewUser2") = CType(gr.FindControl("txtNewUser2"), TextBox).Text
                    drs(0)("OtherUser") = CType(gr.FindControl("txtNewOtherUser"), TextBox).Text
                    drs(0)("asset_ownerID") = HttpUtility.HtmlDecode(gr.Cells(8).Text).Trim


                End If

            End If



        Next
        Dim approveoa As String

        Dim dr2 As DataRow
        For Each gr As GridViewRow In gvMain.Rows
            If CType(gr.FindControl("chkReturn"), CheckBox).Checked Then
                '檢杳是否已在datable 中

                Try
                    dr2 = uDAO.GetAUOFORM87_CODELIST(ddlFactory.SelectedValue, "2").Rows(0)

                    approveoa = dr2("approveroa").ToString
                Catch ex As Exception
                    ShowMessageForAjax(btnClose, GetLocalResourceObject("errSite"))
                    RaiseEvent onShow(btnClose, New EventArgs())
                    Return False
                End Try

                drs = myDT.Select("asset_serialNo='" + gr.Cells(2).Text + "'")
                If drs.Length = 0 Then
                    drs1 = myOtherDT.Select("asset_serialNo='" + gr.Cells(2).Text + "'") ''檢查是否在移轉資料中
                    If drs1.Length = 0 Then
                        'If CType(gr.FindControl("txtNewOwner"), TextBox).Text.Trim = "" Then
                        '    ShowMessageForAjax(btnConfirm, GetLocalResourceObject("NewOwnerEmpty"))
                        '    RaiseEvent onShow(btnConfirm, New EventArgs())
                        '    Return False
                        'End If
                        dr = myDT.NewRow()
                        dr("asset_serialNo") = HttpUtility.HtmlDecode(gr.Cells(2).Text).Trim ''資產編號
                        dr("Def_Name") = HttpUtility.HtmlDecode(gr.Cells(1).Text).Trim '資產類別
                        dr("ModiType") = "2"  '繳回
                        dr("asset_owner") = HttpUtility.HtmlDecode(gr.Cells(3).Text).Trim
                        dr("User2") = HttpUtility.HtmlDecode(gr.Cells(4).Text).Trim
                        dr("NewOwner") = approveoa
                        dr("NewUser2") = approveoa
                        dr("OtherUser") = ""
                        dr("asset_ownerID") = HttpUtility.HtmlDecode(gr.Cells(8).Text).Trim
                        myDT.Rows.Add(dr)
                    End If
                Else

                    drs(0)("asset_serialNo") = HttpUtility.HtmlDecode(gr.Cells(2).Text).Trim ''資產編號
                    drs(0)("Def_Name") = HttpUtility.HtmlDecode(gr.Cells(1).Text).Trim '資產類別
                    drs(0)("ModiType") = "2"   '繳回
                    drs(0)("asset_owner") = HttpUtility.HtmlDecode(gr.Cells(3).Text).Trim
                    drs(0)("User2") = HttpUtility.HtmlDecode(gr.Cells(4).Text).Trim
                    drs(0)("NewOwner") = approveoa
                    drs(0)("NewUser2") = approveoa
                    drs(0)("OtherUser") = ""
                    drs(0)("asset_ownerID") = HttpUtility.HtmlDecode(gr.Cells(8).Text).Trim

                End If

            End If



        Next


        'ShowMessageForAjax(btnClose, GetLocalResourceObject("Finish"))

        Return True
    End Function
    Protected Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        'InsertData()
    End Sub

    Protected Sub btnPreviouse_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If InsertData() = False Then
            Exit Sub
        End If
        RaiseEvent onShowPreviouse(sender, New EventArgs())

    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If InsertData() = False Then
            Exit Sub
        End If
        RaiseEvent onShowNext(sender, New EventArgs())

    End Sub

    Protected Sub gvMain_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvMain.PageIndexChanged
        RaiseEvent onShow(sender, New EventArgs())
    End Sub

    Protected Sub chkTransferHead_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim chk As Boolean = CType(sender, CheckBox).Checked
        For Each gr As GridViewRow In gvMain.Rows
            CType(gr.FindControl("chkTransfer"), CheckBox).Checked = chk

            If chk Then
                CType(gr.FindControl("chkReturn"), CheckBox).Checked = False
                If CType(gr.FindControl("txtNewOwner"), TextBox).Text.Trim = "" Then
                    CType(gr.FindControl("txtNewOwner"), TextBox).Text = txtNewOwner.Text
                End If
                If CType(gr.FindControl("txtNewUser2"), TextBox).Text.Trim = "" Then
                    CType(gr.FindControl("txtNewUser2"), TextBox).Text = txtNewUser2.Text
                End If
                If CType(gr.FindControl("txtNewOtherUser"), TextBox).Text.Trim = "" Then
                    CType(gr.FindControl("txtNewOtherUser"), TextBox).Text = txtNewOtherUser.Text
                End If
            End If
        Next
        If chk Then
            CType(gvMain.HeaderRow.FindControl("chkReturnHead"), CheckBox).Checked = False
        End If

        RaiseEvent onShow(sender, New EventArgs())
    End Sub

    Protected Sub chkReturnHead_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim chk As Boolean = CType(sender, CheckBox).Checked
        For Each gr As GridViewRow In gvMain.Rows
            CType(gr.FindControl("chkReturn"), CheckBox).Checked = chk
            If chk Then
                CType(gr.FindControl("chkTransfer"), CheckBox).Checked = False
                If CType(gr.FindControl("txtNewOwner"), TextBox).Text.Trim = "" Then
                    CType(gr.FindControl("txtNewOwner"), TextBox).Text = txtNewOwner.Text
                End If
                If CType(gr.FindControl("txtNewUser2"), TextBox).Text.Trim = "" Then
                    CType(gr.FindControl("txtNewUser2"), TextBox).Text = txtNewUser2.Text
                End If
                If CType(gr.FindControl("txtNewOtherUser"), TextBox).Text.Trim = "" Then
                    CType(gr.FindControl("txtNewOtherUser"), TextBox).Text = txtNewOtherUser.Text
                End If
            End If
        Next
        If chk Then
            CType(gvMain.HeaderRow.FindControl("chkTransferHead"), CheckBox).Checked = False
        End If

        RaiseEvent onShow(sender, New EventArgs())
    End Sub

    Protected Sub chkTransfer_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If CType(sender, CheckBox).Checked Then
            Dim asset_serialNo As String = CType(sender, CheckBox).ToolTip
            For Each gr As GridViewRow In gvMain.Rows
                If HttpUtility.HtmlDecode(gr.Cells(2).Text).Trim = asset_serialNo.Trim Then
                    If CType(gr.FindControl("txtNewOwner"), TextBox).Text.Trim = "" Then
                        CType(gr.FindControl("txtNewOwner"), TextBox).Text = txtNewOwner.Text
                    End If
                    If CType(gr.FindControl("txtNewUser2"), TextBox).Text.Trim = "" Then
                        CType(gr.FindControl("txtNewUser2"), TextBox).Text = txtNewUser2.Text
                    End If
                    If CType(gr.FindControl("txtNewOtherUser"), TextBox).Text.Trim = "" Then
                        CType(gr.FindControl("txtNewOtherUser"), TextBox).Text = txtNewOtherUser.Text
                    End If
                    CType(gr.FindControl("chkReturn"), CheckBox).Checked = False


                End If


            Next

        End If


        RaiseEvent onShow(sender, New EventArgs())
        RunClientScriptForAjax(Me.Page, "setTimeout(""$get('" & sender.ClientID & "').focus(); "", 10);")
    End Sub

    Protected Sub chkReturn_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim approveoa As String
        Dim uDao As OAMS.DAO.UsersDAO = New OAMS.DAO.UsersDAO(dbName)
        Dim dr2 As DataRow
        If CType(sender, CheckBox).Checked Then
            Dim asset_serialNo As String = CType(sender, CheckBox).ToolTip
            For Each gr As GridViewRow In gvMain.Rows
                If HttpUtility.HtmlDecode(gr.Cells(2).Text).Trim = asset_serialNo.Trim Then

                    Try
                        dr2 = uDao.GetAUOFORM87_CODELIST(ddlFactory.SelectedValue, "2").Rows(0)
                       
                        approveoa = dr2("approveroa").ToString
                    Catch ex As Exception
                        ShowMessageForAjax(btnClose, GetLocalResourceObject("errSite"))
                        RaiseEvent onShow(sender, New EventArgs())
                        Return
                    End Try

                    CType(gr.FindControl("txtNewOwner"), TextBox).Text = approveoa


                    CType(gr.FindControl("txtNewUser2"), TextBox).Text = approveoa


                    CType(gr.FindControl("chkTransfer"), CheckBox).Checked = False


                End If


            Next

        End If

        RaiseEvent onShow(sender, New EventArgs())
        RunClientScriptForAjax(Me.Page, "setTimeout(""$get('" & sender.ClientID & "').focus(); "", 10);")
    End Sub

    Protected Sub gvMain_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvMain.DataBound
        RaiseEvent onShow(sender, New EventArgs())
    End Sub

    Protected Sub gvMain_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvMain.PageIndexChanging
        If InsertData() = False Then
            e.Cancel = True
            Exit Sub
        End If
    End Sub

    Protected Sub gvMain_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvMain.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim asset_serialNo As String = e.Row.Cells(2).Text
            Dim drs() As DataRow = myDT.Select("ModiType='1' and asset_serialNo='" + asset_serialNo + "'")
            If drs.Length > 0 Then
                CType(e.Row.FindControl("chkTransfer"), CheckBox).Checked = True
                CType(e.Row.FindControl("txtNewOwner"), TextBox).Text = drs(0)("NewOwner").ToString()
                CType(e.Row.FindControl("txtNewUser2"), TextBox).Text = drs(0)("NewUser2").ToString()
                CType(e.Row.FindControl("txtNewOtherUser"), TextBox).Text = drs(0)("OtherUser").ToString()
            End If
            Dim drs2() As DataRow = myDT.Select("ModiType='2' and asset_serialNo='" + asset_serialNo + "'")
            If drs2.Length > 0 Then
                CType(e.Row.FindControl("chkReturn"), CheckBox).Checked = True
                CType(e.Row.FindControl("txtNewOwner"), TextBox).Text = drs2(0)("NewOwner").ToString()
                CType(e.Row.FindControl("txtNewUser2"), TextBox).Text = drs2(0)("NewUser2").ToString()
                CType(e.Row.FindControl("txtNewOtherUser"), TextBox).Text = drs2(0)("OtherUser").ToString()
            End If

        End If
    End Sub

    Protected Sub btnConfirm_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConfirm.Click

    End Sub
End Class
